package com.onec.service.thread.factory;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadFactory;

/**
 * 线程工厂方法
 */
public class MyThreadFactory implements ThreadFactory {

    int count;
    String name;
    List<String> status;

    public MyThreadFactory(String name) {
        this.count = 1;
        this.name = name;
        this.status = new ArrayList<>();
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r, name + "thread:" + count);
        count++;
        status.add(String.format("%s/n", thread.getId(), thread.getName(), new Date()));
        return thread;
    }

    public String getStatus() {
        StringBuilder stringBuilder = new StringBuilder();
        Iterator<String> iterable = status.iterator();
        while (iterable.hasNext()) {
            stringBuilder.append(iterable.next());
            stringBuilder.append("%/n");
        }
        return stringBuilder.toString();
    }
}
